import {
  fileUrl
} from "../pages/network/index"
let app = getApp()
export function uploadFile() {
  return new Promise((resolve, rejects) => {
    let arrFile = []
    wx.chooseMessageFile({
      type: "file",
      count: 10,
      extension: app.globalData.extension,
      fail(v) {
        console.log(v)
      },
      success: (arr) => {
        console.log(arr)
        wx.showLoading({
          title: '文件上传中...',
          mask: true
        })
        arr.tempFiles.forEach((item, index) => {
          let suffixArray = item.name.split('.')
          let suffix = suffixArray[suffixArray.length - 1]
          wx.uploadFile({
            filePath: item.path,
            name: 'img',
            url: fileUrl,
            success: (v) => {
              if (v.statusCode != 200) {
                wx.showToast({
                  title: '文件上传失败！',
                  icon: "none"

                })
                rejects()
              } else {
                let file = JSON.parse(v.data).data
                arrFile.push({
                  url: file,
                  fileType: suffix,
                  fileName: item.name
                })
                if (arrFile.length == arr.tempFiles.length) {
                  resolve(arrFile)
                  wx.hideLoading({
                    success: (res) => {},
                  })
                }
              }
            },
            fail(v) {
              console.log(v)
            }
          })
        })

      }
    })


  })
}
//上传图片
export function uploadPicture(count = 10) {
  return new Promise((resolve, rejects) => {
    let arrpicTrue = []
    wx.chooseImage({
      count: count,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      complete(e) {},
      success(res) {
        console.log(res,'reeeeee');
        
        // tempFilePath可以作为img标签的src属性显示图片
        const tempFilePaths = res.tempFiles
        //获取文件大小
        let ratio = 80
        tempFilePaths.forEach(element => {
          if (element.size > 20971520) {
            wx.showToast({
              title: '图片限制为20M',
              icon: 'none'
            })
            return
          } else if (element.size > 10485760)
            ratio = 10
          else if (element.size > 5242880 && element.size < 10485760) {
            ratio = 20
          }
          //压缩图片，只能在真机上使用，模拟器不支持

          wx.compressImage({
            src: element.path,
            quality: ratio,
            success: function (res) {
              wx.showLoading({
                title: '图片上传中...',
              })
              wx.uploadFile({
                filePath: res.tempFilePath,
                name: 'img',
                url: fileUrl,
                success: function (v) {
                  wx.hideLoading({
                    success: (res) => {},
                  })

                  if (v.statusCode != 200) {
                    wx.showToast({
                      title: '图片上传失败',
                      icon: 'none'
                    })
                    return
                  }
                  let imageUrl = JSON.parse(v.data).data

                  // taskNumber[index].image.push(imageUrl)

                  // _this.setData({
                  //   taskNumber
                  // })
                  arrpicTrue.push({
                    url: imageUrl,
                    fileType: 'image'
                  })
                  if (arrpicTrue.length == tempFilePaths.length) {
                    resolve(arrpicTrue)
                  }
                },
                fail: function () {
                  wx.showToast({
                    title: '图片上传失败',
                    icon: 'none'
                  })
                  rejects()
                }
              })
            },
            fail: function (v) {
              wx.showToast({
                title: '图片压缩失败',
                icon: 'none'
              })
              rejects()
            }
          })
        })
      },
      fail: function (v) {
        rejects()
      }
    })
  })
}


//预览图片
export function prePicTure(e, arr) {
  console.log(e)
  let file = e.currentTarget.dataset.file
  let arrImage = arr.map(item => {
    if (item.source == 'image') {
      return item.url
    }
  })
  if (file.source != 'image') {
    wx.showLoading({
      title: '正在打开文件',
    })
    wx.downloadFile({
      url: file.url,
      success: (res) => {
        const filePath = res.tempFilePath
        wx.openDocument({
          filePath: filePath,
          success: function (res) {
            wx.hideLoading({
              success: (res) => {},
            })
          },
          fail: function (v) {
            wx.showToast({
              title: '打开文件失败，请重试 ',
              icon: "none"
            })
          }
        })
      }
    })
  } else {
    wx.previewImage({
      current: file.url,
      urls: arrImage
    })
  }
}